package com.administration.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.administration.controller.AdministrationController;
import com.administration.entity.Access;
import com.core.managedBean.BaseBean;
import com.core.util.Utils;

@ManagedBean
@ViewScoped
public class AccessBean extends BaseBean {
	private static final long serialVersionUID = 1L;
	private Access access;
	List<Access> accessList;
//	private AdministrationService administrationService = new AdministrationServiceImpl();
	AdministrationController administrationController;;
	@PostConstruct
	public void loadData(){
		if (administrationController == null) {
			administrationController = new AdministrationController();
		}
		access= new Access();
		if(Utils.isEmpty(accessList)){
			loadallAccess();
		}
	}
	public void saveAccess() {
		try {
			if (access.getRecid() == null) {
				Long accessRecId;
				accessRecId = administrationController.addAccess(this.access);
				this.access.setRecid(accessRecId);

			} else {
				administrationController.updateAccess(this.access);
			}
			loadallAccess();
			super.addInfoMessage("Successfully Saved Access "+this.access.getName());
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("Error Exist");
		}
	}

	public void deleteAccess() {
		try {
			administrationController.delateAccess(this.access);
			super.addInfoMessage("Successfully Deleted Access "+this.access.getName());
			loadallAccess();
		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
		}
	}

	public void loadallAccess() {
		try {
			this.accessList = administrationController.getAllAccess();
		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
		}
	}
	public void toAddAccess(){
		this.access.setRecid(null);
		this.access.setName(null);
	}
	public void toUpdateAccess(){
		//this method does't do any thing 
		// only to access the beaen
	}
//---------------------------------------------------------//
	public Access getAccess() {
		return access;
	}

	public void setAccess(Access access) {
		this.access = access;
	}

	public List<Access> getAccessList() {
		return accessList;
	}

	public void setAccessList(List<Access> accessList) {
		this.accessList = accessList;
	}

}
